package com.spsoft.core.utils;

import cn.hutool.core.codec.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class FileStreamUtil {


    //将byte转换成文件
    public static void bytesToFile(byte[] bytes, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {

            file = new File(fileName);
            if (!file.getParentFile().exists()){
                //文件夹不存在 生成
                file.getParentFile().mkdirs();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //文件转换成InputStream
    public static byte[] fileToBytes(String filePath) {
        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;
        try {
            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytesArray;
    }

    //字符串转换成文件流
    public static void stirngFile(String hexString, String targetPath) {
        byte[] buffer;
        FileOutputStream out = null;
        FileOutputStream out2 = null;
        try {
            Base64 base64 = new Base64();
            //解码
            buffer = base64.decode(hexString);
            System.out.println("BASE64转译byte111:" + buffer.length);
            out = new FileOutputStream(targetPath);
            out.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out2 != null) {
                try {
                    out2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    //文件流转换成字符串
    public static String fileString(String path) {
        File file = new File(path);
        FileInputStream fis = null;
        StringBuffer content = new StringBuffer();
        try {
            fis = new FileInputStream(file);
            int length = 2 * 1024 * 1024;
            byte[] byteAttr = new byte[length];
            int byteLength = -1;

            while ((byteLength = fis.read(byteAttr, 0, byteAttr.length)) != -1) {

                String encode = "";
                if (byteLength != byteAttr.length) {
                    byte[] temp = new byte[byteLength];
                    System.arraycopy(byteAttr, 0, temp, 0, byteLength);
                    //使用BASE64转译
                    Base64 base64 = new Base64();
                    encode = base64.encode(temp);
                    //encode = new BASE64Encoder().encode(temp);
                    content.append(encode);
                } else {
                    Base64 base64 = new Base64();
                    encode = base64.encode(byteAttr);
                    //encode = new BASE64Encoder().encode(byteAttr);
                    content.append(encode);
                }
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return content.toString();
    }

    /**
     * 下载文件
     *
     * @param bytes
     */
    public static void downloadPdfFile(byte[] bytes, String fileName,String downloadType) {
        HttpServletRequest request = ServletUtils.getHttpServletRequest();
        HttpServletResponse response = ServletUtils.getHttpServletResponse();
        try {
            // 清空response
            response.reset();
            response.setCharacterEncoding(request.getCharacterEncoding());
//            response.setContentType("application/pdf");
            String contentType = "application/"+downloadType;
            response.setContentType(contentType);
            //attachment --- 作为附件下载
            //inline --- 在线打开
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Access-Control-Allow-Origin","*");
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e) {

        }
    }
}
